package miiteen.demo.HB2012Service;


import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.*;

@org.springframework.stereotype.Service("HB2012Service")
public class Service {
    private static com.HB2012.client.BasicHttpBinding_IHB2012APIStub binding;
    static{
        binding=HB2012Factory.getHB2012HttpBinding();
    }

 public  Map<String,Object> getResource(String start,String count) throws Exception{
        Map<String,Object> m=new HashMap<>();

    // binding.setTimeout(60000);

     String value = binding.getALLCommonFileList("ZS_DUBAN",Integer.parseInt(start),Integer.parseInt(count), new String(), new String(), new String());
     if("".equals(value)){
         return null;
     }
     Document doc=null;
     doc= DocumentHelper.parseText(value);
     Element root=doc.getRootElement();
     Element responseData=root.element("ResponseData");
     m.put("TotalCount",responseData.elementText("TotalCount"));
     Element commonFileList=responseData.element("CommonFileListConverter");
    List<Element> itemList=commonFileList.elements("Item");
    List<XmlInfo> resourceList=new ArrayList<>();
     SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
     SimpleDateFormat sdf1=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
     for (Element ele:itemList
          ) {
        XmlInfo xmlInfo=new XmlInfo();
         String resourceID=ele.elementText("ID");
         xmlInfo.setId(resourceID);
         xmlInfo.setTitle(ele.elementText("TaskTitle"));
         xmlInfo.setCompletedFlag(ele.elementText("CompletedFlag"));
         xmlInfo.setEmergency(ele.elementText("Emergency"));
         xmlInfo.setSn(ele.elementText("SN"));
         xmlInfo.setUrl(ele.elementText("URL").replace("localhost","10.89.8.18"));
         String value2=binding.resourceDetail("ZS_DUBAN",resourceID);
         if(!"".equals(value2)){
             doc=DocumentHelper.parseText(value2);
             Element root2=doc.getRootElement();
             Element baseInfo=root2.element("ResponseData").element("BaseInfo");
             String expireTime=baseInfo.elementText("ExpireTime");
             xmlInfo.setExpireTime(sdf.format(sdf1.parse(expireTime)));
             Element ZBDept=baseInfo.element("ZBDept");
             if(ZBDept.elements().size()!=0){
                 xmlInfo.setZBDept(ZBDept.element("Item").elementText("ToDeptName"));
             }
             resourceList.add(xmlInfo);
         }

     }
     m.put("resourceList",resourceList);

     return m;
 }

    public  Map<String,Object> getDBInfo(String start,String count) throws Exception{
        Map<String,Object> m=new HashMap<>();

        // binding.setTimeout(60000);

        String value =  binding.getDBBaseinfoList(Integer.parseInt(start), Integer.parseInt(count), new String(), new String(), new String(), new String(), new String(), new String(), new String(), new String());
        if("".equals(value)){
            return null;
        }
        Document doc=null;
        doc= DocumentHelper.parseText(value);
        Element root=doc.getRootElement();
        Element responseData=root.element("ResponseData");
        m.put("TotalCount",responseData.elementText("TotalCount"));
        Element dBBaseinfoList=responseData.element("DBBaseinfoList");
        List<Element> itemList=dBBaseinfoList.elements("Item");
        List<XmlInfo> resourceList=new ArrayList<>();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf1=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String url="http://10.89.8.18/HBPlusWebApp/HidealDB/DBForm/DBFrontController.aspx?";
        for (Element ele:itemList
        ) {
            XmlInfo xmlInfo=new XmlInfo();
            String resourceID=ele.elementText("ResourceID");
            xmlInfo.setId(resourceID);
            xmlInfo.setTitle(ele.elementText("TaskTitle"));
            String expireTime=ele.elementText("ExpireTime");
            xmlInfo.setExpireTime(sdf.format(sdf1.parse(expireTime)));
            xmlInfo.setEmergency(ele.elementText("Emergency").replaceAll("\\s*",""));
            xmlInfo.setSn(ele.elementText("SN_TITLE")+"["+ele.elementText("SN_YEAR")+"]"+ele.elementText("SN_NO")+"号");
            xmlInfo.setUrl(url+"resourceID="+resourceID);
            Element ZBDept=ele.element("ZBDept");
            if(ZBDept.elements().size()!=0) {
                xmlInfo.setZBDept(ZBDept.element("Item").elementText("ToDeptName"));
                xmlInfo.setCompletedFlag(ZBDept.element("Item").elementText("Completed"));
            }
            resourceList.add(xmlInfo);
        }
        m.put("resourceList",resourceList);

        return m;
    }







 public  Map<String,Object> getCommonFileList(String start,String count, String title, String name) throws  Exception {
     Map<String, Object> m = new HashMap<>();
     String value = binding.getCommonFileList(name, Integer.parseInt(start), Integer.parseInt(count), new String(), new String(), title);
     if("".equals(value)){
         return null;
     }
     Document doc = null;
     doc = DocumentHelper.parseText(value);
     Element root = doc.getRootElement();
     Element responseData = root.element("ResponseData");
     m.put("TotalCount", responseData.elementText("TotalCount"));
     Element commonFileList = responseData.element("CommonFileListConverter");
     List<Element> itemList = commonFileList.elements("Item");
     List<XmlInfo> resourceList = new ArrayList<>();
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
     SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
     for (Element ele : itemList
     ) {
         Map<String, String> map = new HashMap<>();
         XmlInfo xmlInfo=new XmlInfo();
         String resourceID = ele.elementText("ID");
         xmlInfo.setId(resourceID);
         xmlInfo.setTitle(ele.elementText("TaskTitle"));
         xmlInfo.setCompletedFlag(ele.elementText("CompletedFlag"));
         xmlInfo.setEmergency(ele.elementText("Emergency"));
         xmlInfo.setSn(ele.elementText("SN"));
         xmlInfo.setUrl(ele.elementText("URL").replace("localhost","10.89.8.18"));
         xmlInfo.setCompletedTime(sdf.format(sdf1.parse(ele.elementText("CompletedTime"))));
         resourceList.add(xmlInfo);
         }
     m.put("resourceList",resourceList);
     return m;
     }

    public  Map<String,Object> getTaskList(String Guid) throws Exception{
        Map<String,Object> m=new HashMap<>();

        // binding.setTimeout(60000);
        String[] applicationNameList=new String[]{"HIDEALNOTICE", "HIDEALCOOPERATION", "HIDEALSW", "HIDEALFW", "HIDEALQB", "ZS_DUBAN", "ZS_NEWS", "HIDEALCAP"};
        String value = binding.getUserTaskListCount(Guid,applicationNameList);
        if("".equals(value)){
            return null;
        }
        Document doc=null;
        doc= DocumentHelper.parseText(value);
        Element root=doc.getRootElement();
        Element responseData=root.element("ResponseData");
        m.put("DaiBanCount",responseData.elementText("DaiBanCount"));
        m.put("DaiYueCount",responseData.elementText("DaiYueCount"));
        return m;
    }


    public  Map<String,Object> getDBList(String start,String count) throws Exception{
        Map<String,Object> m=new HashMap<>();
        start="0";
        count="5";
        // binding.setTimeout(60000);
        String value = binding.getDBTableinfoList(Integer.parseInt(start),Integer.parseInt(count), new String(), new String(), new String(), new String(), new String(), new String());

        if("".equals(value)){
            return null;
        }
        Document doc=null;

        return m;
    }
}
